package com.woniu.his.sms.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniu.his.exception.HisException;
import com.woniu.his.result.CommonResultCode;
import com.woniu.his.result.Result;
import com.woniu.his.sms.client.SmsSkdClient;
import com.woniu.his.sms.client.param.*;
import com.woniu.his.sms.client.result.*;
import com.woniu.his.sms.infra.model.Skd;
import com.woniu.his.sms.common.SmsResultCode;
import com.woniu.his.sms.infra.model.Skd;
import com.woniu.his.sms.infra.model.SkdRule;
import com.woniu.his.sms.service.SkdRuleService;
import com.woniu.his.sms.service.SkdService;
import com.woniu.his.sms.service.dto.*;
import com.woniu.his.sms.service.param.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version: java version 1.8
 * @Author: Mr tao
 * @description:
 * @date: 2022-11-26 15:17
 */
@RestController
@RequestMapping("sms")
@Slf4j
public class SmsSkdApilmpl implements SmsSkdClient {

    @Resource
    private SkdService skdService;


    /**
     * 新建排班规则
     *
     * @param skdRuleParam
     * @return
     */
    @Override
    public Result createRule(SkdRuleParam skdRuleParam) {
        //参数校验+类型转换
        log.info("创建规则:{}", skdRuleParam);
        SkdRuleServerParam param = BeanUtil.copyProperties(skdRuleParam, SkdRuleServerParam.class);
        boolean b = skdService.createRule(param);
        return b ? Result.getInstance() : Result.getInstance().setCode(CommonResultCode.GENERAL_FAIL.getCode()).setMsg("添加失败");
    }
    /**
     * 根据id删除排班规则 和排班项目
     *
     * @param ids
     * @return
     */
    @Override
    public Result deleteRule(List<Long> ids) {
        log.info("根据id删除排班规则 和排班项目:{}", ids);
        boolean b = skdService.deleteRule(ids);
        return b ? Result.getInstance() : Result.getInstance().setCode(CommonResultCode.GENERAL_FAIL.getCode()).setMsg("删除失败");
    }

    /**
     * 修改排班信息
     * @param skdRuleParam
     * @return
     */
    @Override
    public Result updateRule(SkdRuleParam skdRuleParam) {
        log.info("更新排班规则项目:{}", skdRuleParam);
        SkdRuleServerParam param = BeanUtil.copyProperties(skdRuleParam, SkdRuleServerParam.class);
        boolean b = skdService.updateRule(param);
        return b ? Result.getInstance() : Result.getInstance().setCode(CommonResultCode.GENERAL_FAIL.getCode()).setMsg("更新失败");

    }

    /**
     * 根据科室表查询排班规则  分页
     *
     * @param param
     * @return
     */
    @Override
    public Result<SkdRulePageResult> queryPageRule(SkdRulePageParam param) {
        log.info("根据科室id查询排班规则分页:{}", param);
        //类型转换
        SkdRulePageServiceParam params = BeanUtil.copyProperties(param, SkdRulePageServiceParam.class);
        SkdRulePageDto dto = skdService.queryPageRule(params);
        SkdRulePageResult skdRulePageResult = BeanUtil.copyProperties(dto, SkdRulePageResult.class);
        return Result.getInstance(SkdRulePageResult.class).setData(skdRulePageResult);
    }
    /**
     * 根据规则id 查询规则
     *
     * @param sdkRuleId
     * @return
     */
    @Override
    public Result<SkuRuleResult> queryRuleDetail(Long sdkRuleId) {
        log.info("根据规则id查询规则详情:{}", sdkRuleId);
        SkuRuleServiceDto dto = skdService.queryRuleDetail(sdkRuleId);
        SkuRuleResult skuRuleResult = BeanUtil.copyProperties(dto, SkuRuleResult.class);
        return Result.getInstance(SkuRuleResult.class).setData(skuRuleResult);
    }


    /**
     * 根据多条排班规则 生成排班记录
     * @param param
     * @return
     */
    @Override
    public Result generateSkd(CreateSkdParam param) {
       log.info("根据多条排班规则 生成排班记录:{}",param);
       //类型转换
       CreateSkdServiceParam createSkdParam = BeanUtil.copyProperties(param,CreateSkdServiceParam.class);
       Boolean b= skdService.generateSkd(createSkdParam);
        return b ? Result.getInstance() : Result.getInstance().setCode(CommonResultCode.GENERAL_FAIL.getCode()).setMsg("生成记录失败");

    }

    //分页查询排班记录
    @Override
    public Result<SkdSkuPageResult> queryPageSkd(SkuSkdPageParam skuSkdPageParam) {
        log.info("分页查询排班记录:{}",skuSkdPageParam);
        //类型转换
        SkuSkdPageServiceParam param = BeanUtil.copyProperties(skuSkdPageParam, SkuSkdPageServiceParam.class);
        SkuSkdPageDto dto =skdService.queryPageSkd(param);
        SkdSkuPageResult skdSkuPageResult = BeanUtil.copyProperties(dto, SkdSkuPageResult.class);
        return Result.getInstance(SkdSkuPageResult.class).setData(skdSkuPageResult);
    }

    /**
     * 查询排班记录 不分页
     * @param skuSkdParam
     * @return
     */
    @Override
    public Result<List<SmsSkdResult>> querySkd(SkuSkdParam skuSkdParam) {
        log.info("不分页查询排班记录:{}",skuSkdParam);
        //类型转换
        SkuSkdServiceParam param = BeanUtil.copyProperties(skuSkdParam, SkuSkdServiceParam.class);
        List<SmsSkdDto> smsSkdDtos = skdService.querySkd(param);
        List<SmsSkdResult> smsSkdResults = BeanUtil.copyToList(smsSkdDtos, SmsSkdResult.class);
        return Result.getInstanceList(SmsSkdResult.class).setData(smsSkdResults);
    }


    /**
     * :新增时候列出的科室下员工信息
     * @param skRuleId
     * @return
     */
    @Override
    public Result<List<SmsSkdRuleItemResult>> listCanSkdStaff(Long skRuleId) {
        log.info("新增时候列出的科室下员工信息:{}",skRuleId);
        List<SmsSkdRuleItemDto> dtoList =skdService.listCanSkdStaff(skRuleId);

        return null;
    }

    @Override
    public Result<SkdResult> checkSkdIsExist(Long skdId) {

        log.info("根据skdId:{}",skdId);
        Skd one = skdService.getById(skdId);
        SkdResult result=BeanUtil.copyProperties(one,SkdResult.class);
        if (one==null) return Result.getInstance(SkdResult.class).setCode(500).setMsg("Skd不存在");
        return Result.getInstance(SkdResult.class).setData(result);
    }

    @Override
    public Result<SkdInfoResult> findSkdInfoById(Long skdId) {
        log.info("新增时候列出的科室下员工信息:{}",skdId);
        Skd skd = skdService.getById(skdId);
        SkdInfoResult skdInfoResult = BeanUtil.copyProperties(skd, SkdInfoResult.class);
        if (skdInfoResult==null) {
            throw  new HisException(SmsResultCode.SKD_NOT_EXITS);
        }
        return Result.getInstance(SkdInfoResult.class).setData(skdInfoResult);
    }

    @Override
    public Result deductions(Long id) {
        log.info("扣减库存:{}",id);
        Skd byId = skdService.getById(id);
        if(byId==null) return Result.getInstance(CommonResultCode.GENERAL_FAIL).setMsg("更新失败,该医生不存在");
        byId.setRemain(byId.getRemain()-1);
        if(byId.getSkLimit()<0) return Result.getInstance(CommonResultCode.GENERAL_FAIL).setMsg("更新失败 剩余名额不足");
        skdService.updateById(byId);
        return Result.getInstance().setMsg("更新成功");
    }
}
